package com.ymttest.business.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;

import com.google.gson.JsonObject;
import com.ymatou.iapi.productlimit.parameter.CatalogLimit;
import com.ymatou.iapi.productlimit.parameter.GetLimitAllowBuyBean;
import com.ymatou.iapi.productlimit.parameter.OperationProductLimit;
import com.ymatou.iapi.productlimit.parameter.OperationProductLimitBean;
import com.ymatou.iapi.productlimit.parameter.ReturnProductLimit;
import com.ymatou.iapi.productlimit.parameter.ReturnProductLimitBean;
import com.ymatou.iapi.productlimit.service.GetLimitAllowBuyCall;
import com.ymatou.iapi.productlimit.service.OperationProductLimitCall;
import com.ymatou.iapi.productlimit.service.ReturnProductLimitCall;
import com.ymatou.iapi.productprice.parameter.ProductAllPriceBean;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymttest.database.mongo.ProductsWapper;
import com.ymttest.database.mongo.jproduct.ActivityProductsWapper;
import com.ymttest.utils.TestDataManager;

public class ProductLimitCallService {
	/**
	 * * 生成一个默认的OperationProductLimitBean
	 * 会创建一个新活动并加入一个随机商品
	 * activityLimit和productLimit在业务上只有且必须一个有值
	 * @param userId
	 * @param activityLimit
	 * @param productLimit
	 * @param buycount 每个商品购买几件
	 * @param count 添加几个商品
	 * @return
	 * @throws Exception
	 */
	public static OperationProductLimitBean initOperationProductLimitBean(int userId, int activityLimit, int productLimit, int buycount,int count) throws Exception {
		ActivityProductsWapper activityProductsWapper = new ActivityProductsWapper();
		ProductsWapper p = new ProductsWapper();
		// 固定sellerid制作静态数据
		int sellerId = 3383;
		
		OperationProductLimitBean operationproductlimitBean = new OperationProductLimitBean();
		operationproductlimitBean.setAppId("autotest");
		operationproductlimitBean.setBuyerId(userId);
		//限购商品list
		List<OperationProductLimit> operationProductLimitList = new ArrayList<>();
		for(int i=0;i<count;i++){
			OperationProductLimit operationProductLimit = new OperationProductLimit();
			//创建新商品 活动如果存在复用活动
			String productId = ProductPriceCallService.getProdPriceWithActivity_NoNewBuyerAndNoIsolation(sellerId).getProductId();
			operationProductLimit.setProductId(productId);
			//获取商品的inaid
			int inaid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("inaid").toString());
			operationProductLimit.setProductInActivityId(inaid);
			//获取商品所在的activityId
			Integer activityId = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			operationProductLimit.setActivityId(activityId);
			//更新活动mongo的limit 并设置参数
			if (activityLimit > 0) {
				p.updateAlimitByInaid(inaid, activityLimit);
				operationProductLimit.setActivityLimit(activityLimit);
				operationProductLimit.setProductInActivityId(null);
			} else if (productLimit > 0) {
				p.updatePlimitByInaid(inaid, productLimit);
				operationProductLimit.setProductInActivityLimit(productLimit);
			}
			//设置购买数量
			operationProductLimit.setBuyCount(buycount);
			//添加到操作list
			operationProductLimitList.add(operationProductLimit);
		}
		operationproductlimitBean.setOperationProductLimitList(operationProductLimitList);
		
		return operationproductlimitBean;
	}
	public static OperationProductLimitBean initOperationProductLimitBean(int userId, int activityLimit, int productLimit, int buycount,int count,int sellerId) throws Exception {
		ActivityProductsWapper activityProductsWapper = new ActivityProductsWapper();
		ProductsWapper p = new ProductsWapper();
		// 固定sellerid制作静态数据
		
		OperationProductLimitBean operationproductlimitBean = new OperationProductLimitBean();
		operationproductlimitBean.setAppId("autotest");
		operationproductlimitBean.setBuyerId(userId);
		//限购商品list
		List<OperationProductLimit> operationProductLimitList = new ArrayList<>();
		for(int i=0;i<count;i++){
			OperationProductLimit operationProductLimit = new OperationProductLimit();
			//创建新商品 活动如果存在复用活动
			String productId = ProductPriceCallService.getProdPriceWithActivity_NoNewBuyerAndNoIsolation(sellerId).getProductId();
			operationProductLimit.setProductId(productId);
			//获取商品的inaid
			int inaid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("inaid").toString());
			operationProductLimit.setProductInActivityId(inaid);
			//获取商品所在的activityId
			Integer activityId = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			operationProductLimit.setActivityId(activityId);
			//更新活动mongo的limit 并设置参数
			if (activityLimit > 0) {
				p.updateAlimitByInaid(inaid, activityLimit);
				operationProductLimit.setActivityLimit(activityLimit);
				//活动activityLimit时ProductInActivityId为0
				operationProductLimit.setProductInActivityId(0);
			} else if (productLimit > 0) {
				p.updatePlimitByInaid(inaid, productLimit);
				operationProductLimit.setProductInActivityLimit(productLimit);
			}
			//设置购买数量
			operationProductLimit.setBuyCount(buycount);
			//添加到操作list
			operationProductLimitList.add(operationProductLimit);
		}
		operationproductlimitBean.setOperationProductLimitList(operationProductLimitList);
		
		return operationproductlimitBean;
	}
	/**操作限购
	 * @param operationproductlimitBean
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void operationProductLimitCall(OperationProductLimitBean operationproductlimitBean) throws ClientProtocolException, IOException{
		OperationProductLimitCall operationproductlimitCall=new OperationProductLimitCall();
		operationproductlimitCall.setData(false,operationproductlimitBean);
		operationproductlimitCall.callService();
	}
	/**
	 * 使用operationproductlimitBean 初始化 GetLimitAllowBuyBean
	 * @param operationproductlimitBean
	 * @return
	 */
	public static GetLimitAllowBuyBean initGetlimitallowbuyBean(OperationProductLimitBean operationproductlimitBean) {
		GetLimitAllowBuyBean getlimitallowbuyBean=new GetLimitAllowBuyBean();
		getlimitallowbuyBean.setAppId("AutoTest");
		getlimitallowbuyBean.setBuyerId(operationproductlimitBean.getBuyerId());
		List<CatalogLimit> catalogList=new ArrayList<>();
		for(OperationProductLimit opl:operationproductlimitBean.getOperationProductLimitList()){
			CatalogLimit cataloglimit=new CatalogLimit();
			cataloglimit.setActivityId(opl.getActivityId());
			cataloglimit.setProductId(opl.getProductId());
			cataloglimit.setProductInActivityId(opl.getProductInActivityId());
			catalogList.add(cataloglimit);
		}
		getlimitallowbuyBean.setCatalogList(catalogList);
		return getlimitallowbuyBean;
	}
	/**
	 * 获取查询结果call对象
	 * @param GetLimitAllowBuyBean
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws InterruptedException 
	 */
	public static GetLimitAllowBuyCall getLimitAllowBuyCall(GetLimitAllowBuyBean GetLimitAllowBuyBean) throws ClientProtocolException, IOException, InterruptedException{
		Thread.sleep(1000);
		GetLimitAllowBuyCall GetLimitAllowBuyCall=new GetLimitAllowBuyCall();
		GetLimitAllowBuyCall.setData(false,GetLimitAllowBuyBean);
		GetLimitAllowBuyCall.callService();
		return GetLimitAllowBuyCall;
	}
	
	/**
	 * 初始化限购返还bean
	 * @param operationproductlimitBean
	 * @return
	 */
	public static ReturnProductLimitBean initReturnProductLimitBean(OperationProductLimitBean operationproductlimitBean,int num) {
		ReturnProductLimitBean ReturnProductLimitBean=new ReturnProductLimitBean();
		ReturnProductLimitBean.setAppId("AutoTest");
		ReturnProductLimitBean.setBuyerId(operationproductlimitBean.getBuyerId());
		List<ReturnProductLimit> ReturnProductLimits=new ArrayList<>();
		for(OperationProductLimit opl:operationproductlimitBean.getOperationProductLimitList()){
			ReturnProductLimit ReturnProductLimit=new ReturnProductLimit();
			ReturnProductLimit.setActivityId(opl.getActivityId());
			ReturnProductLimit.setProductId(opl.getProductId());
			//当getProductInActivityLimit>0 活动商品限购时 设置getProductInActivityId
			if(opl.getProductInActivityLimit()!=null&&opl.getProductInActivityLimit()>0){
				ReturnProductLimit.setProductInActivityId(opl.getProductInActivityId());
			}
			ReturnProductLimit.setNumber(num);
			ReturnProductLimits.add(ReturnProductLimit);
		}
		ReturnProductLimitBean.setReturnProductLimitList(ReturnProductLimits);
		return ReturnProductLimitBean;
	}
	/**返还限购
	 * @param ReturnProductLimitBean
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static ReturnProductLimitCall returnProductLimitCall(ReturnProductLimitBean ReturnProductLimitBean) throws ClientProtocolException, IOException{
		ReturnProductLimitCall ReturnProductLimitCall=new ReturnProductLimitCall();
		ReturnProductLimitCall.setData(false,ReturnProductLimitBean);
		ReturnProductLimitCall.callService();
		return ReturnProductLimitCall;
	}
	public static OperationProductLimitBean initOperationProductLimitBean(Integer userId, int activityLimit, int productLimit, int buycount,
			List<ProductAllPriceBean> productAllPriceBeanList) throws NumberFormatException, Exception {
		ActivityProductsWapper activityProductsWapper = new ActivityProductsWapper();
		ProductsWapper p = new ProductsWapper();
		// 固定sellerid制作静态数据
		int sellerId = 3383;
		
		OperationProductLimitBean operationproductlimitBean = new OperationProductLimitBean();
		operationproductlimitBean.setAppId("autotest");
		operationproductlimitBean.setBuyerId(userId);
		//限购商品list
		List<OperationProductLimit> operationProductLimitList = new ArrayList<>();
		for(ProductAllPriceBean productAllPriceBean:productAllPriceBeanList){
			OperationProductLimit operationProductLimit = new OperationProductLimit();
			String productId=productAllPriceBean.getProductId();
			//创建新商品 活动如果存在复用活动
			operationProductLimit.setProductId(productId);
			//获取商品的inaid
			int inaid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("inaid").toString());
			operationProductLimit.setProductInActivityId(inaid);
			//获取商品所在的activityId
			Integer activityId = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			operationProductLimit.setActivityId(activityId);
			//更新活动mongo的limit 并设置参数
			if (activityLimit > 0) {
				p.updateAlimitByInaid(inaid, activityLimit);
				operationProductLimit.setActivityLimit(activityLimit);
				operationProductLimit.setProductInActivityId(null);
			} else if (productLimit > 0) {
				p.updatePlimitByInaid(inaid, productLimit);
				operationProductLimit.setProductInActivityLimit(productLimit);
			}
			//设置购买数量
			operationProductLimit.setBuyCount(buycount);
			//添加到操作list
			operationProductLimitList.add(operationProductLimit);
		}
		operationproductlimitBean.setOperationProductLimitList(operationProductLimitList);
		
		return operationproductlimitBean;
	}
}
